/**
 * Copyright (c) 2011-2014, junchen (homeanter@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package xin.tianhui.cloud.weblog.web;


import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import lombok.extern.slf4j.Slf4j;
import xin.tianhui.cloud.oss.model.dto.FileDTO;
import xin.tianhui.cloud.oss.model.dto.UploadFileDTO;
import xin.tianhui.cloud.oss.service.OssService;
import xin.tianhui.cloud.oss.service.aliyun.impl.OssServiceImpl;

/**
 * @author junchen homeanter@163.com
 * @Data 2018-04-16 21:10:31
 */
@Slf4j
@Controller
public class MavonEditorController {
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ResponseBody
	public String upload(HttpServletRequest request) {
		try {
			MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request,
					MultipartHttpServletRequest.class);
			MultiValueMap<String, MultipartFile> map = multipartRequest.getMultiFileMap();
			for (String key : map.keySet()) {
				MultipartFile file = multipartRequest.getFile(key);
				OssService ossService = new OssServiceImpl();
				UploadFileDTO uploadFile = new UploadFileDTO();
				uploadFile.setName(file.getOriginalFilename());
				uploadFile.setStream(file.getInputStream());
				FileDTO dto = ossService.fileUpload(uploadFile);
				return dto.getUrl();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "上传成功";
	}
}
